package com.netmi.mysocket;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.netmi.mysocket.client.TcpClient;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private String TAG_log = "main_Socket";
    private TextView tv_state,tv_receive;
    private EditText et_ip,et_port,et_send;
    private Button btn_connect,btn_disconnect,btn_send,btn_clear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_state = findViewById(R.id.tv_state);
        tv_receive = findViewById(R.id.tv_receive);
        et_ip = findViewById(R.id.et_ip);
        et_port = findViewById(R.id.et_port);
        et_send = findViewById(R.id.et_send);
        btn_connect = findViewById(R.id.btn_connect);
        btn_disconnect = findViewById(R.id.btn_disconnect);
        btn_send = findViewById(R.id.btn_send);
        btn_clear = findViewById(R.id.btn_clear);


        initDataReceiver();
        initListener();
    }

    private void initListener(){
        //socket connect
        btn_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ip = et_ip.getText().toString();
                String port = et_port.getText().toString();

                if(TextUtils.isEmpty(ip)){
                    Toast.makeText(MainActivity.this,"IP地址为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(port)){
                    Toast.makeText(MainActivity.this,"端口号为空",Toast.LENGTH_SHORT).show();
                    return;
                }

                connect(ip, Integer.parseInt(port));
            }
        });

        //socket disconnect
        btn_disconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                disconnect();
            }
        });

        //socket send
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TcpClient.getInstance().isConnect()) {
                    byte[] data = et_send.getText().toString().getBytes();
                    send(data);
                } else {
                    Toast.makeText(MainActivity.this,"尚未连接，请连接Socket",Toast.LENGTH_SHORT).show();
                }
            }
        });

        //clear receive
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_receive.setText("");
            }
        });
    }

    /**
     * socket data receive
     * */
    private void initDataReceiver(){
        TcpClient.getInstance().setOnDataReceiveListener(dataReceiveListener);
    }

    /**
     * socket connect
     * */
    private void connect(String ip, int port){
        TcpClient.getInstance().connect(ip, port);
    }

    /**
     * socket disconnect
     * */
    private void disconnect(){
        TcpClient.getInstance().disconnect();
        tv_state.setText("未连接");
    }

    /**
     * socket send
     * */
    private void send(byte[] data){
        TcpClient.getInstance().sendByteCmd(data,1001);
    }


    /**
     * socket data receive
     * data(byte[]) analyze
     * */
    private TcpClient.OnDataReceiveListener dataReceiveListener = new TcpClient.OnDataReceiveListener() {
        @Override
        public void onConnectSuccess() {
            Log.i(TAG_log,"onDataReceive connect success");
            tv_state.setText("已连接");
        }

        @Override
        public void onConnectFail() {
            Log.e(TAG_log,"onDataReceive connect fail");
            tv_state.setText("未连接");
        }

        @Override
        public void onDataReceive(byte[] buffer, int size, int requestCode) {
            //获取有效长度的数据
            byte[] data = new byte[size];
            System.arraycopy(buffer, 0, data, 0, size);

//            final String oxValue = Arrays.toString(HexUtil.Byte2Ox(data));
            final String oxValue = ByteTransformUtil.bytesToHex(data);

            Log.i(TAG_log,"onDataReceive requestCode = "+requestCode + ", content = "+oxValue);

            tv_receive.setText(tv_receive.getText().toString() + oxValue + "\n");

        }
    };



    @Override
    protected void onDestroy() {
        TcpClient.getInstance().disconnect();
        super.onDestroy();
    }
}
